import java.util.Scanner;
import java.util.Stack;

public class Solution4 {
    static Stack<Long> stack1=new Stack<>();
    static Stack<Long> stack2=new Stack<>();
    public static void main(String[] args) {
        Scanner s=new Scanner(System.in);
        long t=s.nextLong();
        s.nextLine();
        while(t-->0){
            String command=s.nextLine();
            switch (command){
                case "peek":
                    peek();
                    break;
                case "poll":
                    poll();
                    break;
                default:
                    add(Long.valueOf(command.split(" ")[1]));
            }
        }
    }

    public static void add(long num){
        stack1.push(num);
    }
    public static void poll(){
        if(stack2.isEmpty()){
            while(!stack1.isEmpty()){
                stack2.push(stack1.pop());
            }
        }
        stack2.pop();
    }
    public static void peek(){
        if(stack2.isEmpty()){
            while(!stack1.isEmpty()){
                stack2.push(stack1.pop());
            }
        }
        System.out.println(stack2.peek());
    }
}
